/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.RadioButton
 *  javafx.scene.control.TextField
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.AnchorPane
 *  javafx.stage.Modality
 *  javafx.stage.Stage
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.util.ObjectUtil;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.saleRecipel.RemoteAuditController;
import com.gys.controller.salesReceipts.RecipelInfoInputController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.controller.salesReceipts.SelectRecipelDrugsController;
import com.gys.data.saleRecipel.GaiaSdSaleRecipelRecordOutData;
import com.gys.data.salesReceipts.GetSalesReceiptsTableOutData;
import com.gys.view.saleRecipel.RemoteAuditlView;
import com.gys.view.salesReceipts.RecipelDrugsView;
import com.gys.view.salesReceipts.RecipelInfoInputView;
import com.gys.view.salesReceipts.SelectRecipelDrugsView;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class RecipelDrugsController
extends BaseController
implements Initializable {
    private static final Logger log = LoggerFactory.getLogger(RecipelDrugsController.class);
    @FXML
    private RadioButton oneSheet;
    @FXML
    private TextField num;
    @FXML
    private RadioButton twoSheet;
    @FXML
    private AnchorPane pane;
    @FXML
    private RadioButton moreSheet;
    @Autowired
    private RecipelInfoInputController recipelInfoInputController;
    @Autowired
    private SelectRecipelDrugsController selectRecipelDrugsController;
    @Autowired
    private RemoteAuditController remoteAuditController;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    public boolean flag = false;
    public int numZhang = 0;
    private Timer timer = new Timer();
    public boolean inputFlag = false;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        CommonUtil.initIcon(RecipelDrugsView.class);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.pane)) {
            return;
        }
        this.oneSheet.setSelected(true);
        this.twoSheet.setSelected(false);
        this.moreSheet.setSelected(false);
        this.num.clear();
        this.numZhang = 1;
        this.timer.schedule(new TimerTask(){

            @Override
            public void run() {
                RecipelDrugsView view = SpringUtil.getBean(RecipelDrugsView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.ENTER) {
                            RecipelDrugsController.this.sureAction();
                        } else if (e.getCode() == KeyCode.ESCAPE) {
                            RecipelDrugsController.this.cancelAction();
                        } else if (e.getCode() == KeyCode.F3) {
                            RecipelDrugsController.this.preExamineAction();
                        } else if (e.getCode() == KeyCode.LEFT) {
                            if (RecipelDrugsController.this.twoSheet.isSelected()) {
                                RecipelDrugsController.this.twoSheet.setSelected(false);
                                RecipelDrugsController.this.oneSheet.setSelected(true);
                                RecipelDrugsController.this.numZhang = 1;
                            } else if (RecipelDrugsController.this.moreSheet.isSelected()) {
                                RecipelDrugsController.this.moreSheet.setSelected(false);
                                RecipelDrugsController.this.num.clear();
                                RecipelDrugsController.this.twoSheet.setSelected(true);
                                RecipelDrugsController.this.numZhang = 2;
                            }
                        } else if (e.getCode() == KeyCode.RIGHT) {
                            if (RecipelDrugsController.this.oneSheet.isSelected()) {
                                RecipelDrugsController.this.oneSheet.setSelected(false);
                                RecipelDrugsController.this.twoSheet.setSelected(true);
                                RecipelDrugsController.this.numZhang = 2;
                            } else if (RecipelDrugsController.this.twoSheet.isSelected()) {
                                RecipelDrugsController.this.twoSheet.setSelected(false);
                                RecipelDrugsController.this.moreSheet.setSelected(true);
                                RecipelDrugsController.this.num.requestFocus();
                            }
                        }
                        e.consume();
                    }
                });
            }
        }, 500L);
    }

    @FXML
    private void oneSheetAction() {
        this.twoSheet.setSelected(false);
        this.moreSheet.setSelected(false);
        this.num.clear();
        this.numZhang = this.oneSheet.isSelected() ? 1 : 0;
    }

    @FXML
    private void twoSheetAction() {
        this.oneSheet.setSelected(false);
        this.moreSheet.setSelected(false);
        this.num.clear();
        this.numZhang = this.twoSheet.isSelected() ? 2 : 0;
    }

    @FXML
    private void moreSheetAction() {
        this.oneSheet.setSelected(false);
        this.twoSheet.setSelected(false);
        if (this.moreSheet.isSelected()) {
            this.num.requestFocus();
        } else {
            this.num.clear();
        }
    }

    private boolean check() {
        if (this.numZhang == 0 || this.moreSheet.isSelected() && ObjectUtil.isEmpty(this.num.getText())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u6570\u91cf\u4e0d\u80fd\u4e3a\u7a7a");
            alert.showAndWait();
            return false;
        }
        if (this.moreSheet.isSelected()) {
            if (!CommonUtil.positiveInteger(this.num.getText())) {
                Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u8f93\u5165\u6b63\u6574\u6570");
                alert.showAndWait();
                return false;
            }
            this.numZhang = Integer.valueOf(this.num.getText());
            if (this.numZhang < 3) {
                Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u70b9\u9009" + this.numZhang + "\u5f20");
                alert.showAndWait();
                return false;
            }
        }
        return true;
    }

    @FXML
    private void sureAction() {
        if (!this.check()) {
            return;
        }
        this.flag = false;
        this.selectRecipelDrugsController.upPaper = false;
        if (this.numZhang == 1) {
            this.recipelInfoInputController.num = 1;
            this.recipelInfoInputController.init();
            CommonUtil.showView(RecipelInfoInputView.class, Modality.APPLICATION_MODAL);
        } else {
            this.selectRecipelDrugsController.numTh = 1;
            this.selectRecipelDrugsController.init();
            CommonUtil.showView(SelectRecipelDrugsView.class, Modality.APPLICATION_MODAL);
        }
    }

    @FXML
    public void cancelAction() {
        this.pane.getScene().getWindow().hide();
    }

    @FXML
    private void preExamineAction() {
        if (!this.check()) {
            return;
        }
        this.flag = true;
        if (this.numZhang == 1) {
            List<GetSalesReceiptsTableOutData> recipelDrugList = this.salesReceiptsMainController.recipelDrugList;
            ArrayList<GaiaSdSaleRecipelRecordOutData> dataList = new ArrayList<GaiaSdSaleRecipelRecordOutData>();
            for (int i = 0; i < recipelDrugList.size(); ++i) {
                GetSalesReceiptsTableOutData item = recipelDrugList.get(i);
                GaiaSdSaleRecipelRecordOutData sdSaleRecipelRecordOutData = new GaiaSdSaleRecipelRecordOutData();
                sdSaleRecipelRecordOutData.setIndex(i + 1);
                sdSaleRecipelRecordOutData.setGssrVoucherId("");
                sdSaleRecipelRecordOutData.setProCode(item.getProCode());
                sdSaleRecipelRecordOutData.setProCommonname(item.getProName());
                sdSaleRecipelRecordOutData.setProSpecs(item.getProSpecs());
                sdSaleRecipelRecordOutData.setProUnit(item.getProUnit());
                sdSaleRecipelRecordOutData.setGssdBatch(item.getPdAndExpData().getGssbBatch());
                sdSaleRecipelRecordOutData.setGssdQty(item.getNum());
                sdSaleRecipelRecordOutData.setUploadImgStatus("\u5426");
                dataList.add(sdSaleRecipelRecordOutData);
            }
            this.remoteAuditController.dataList = dataList;
            this.remoteAuditController.init();
            CommonUtil.showView(RemoteAuditlView.class, Modality.APPLICATION_MODAL);
        } else {
            this.selectRecipelDrugsController.upPaper = true;
            this.selectRecipelDrugsController.numTh = 1;
            this.selectRecipelDrugsController.init();
            CommonUtil.showView(SelectRecipelDrugsView.class, Modality.APPLICATION_MODAL);
        }
    }
}

